//
//  FXCDetailViewController.m
//  XinLangFeiBo
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FXCDetailViewController.h"
#import "FXCAccount.h"
#import "FXCNetWorkTool.h"

@interface FXCDetailViewController ()
@property (nonatomic, strong) UITextField *textView;
@property (nonatomic, strong) UIButton *sendBtn;
@end

@implementation FXCDetailViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    

    
    
    [self setUpTextView];
    [self setUpNavBar];
//添加观察者
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(textFieldTextDidChange:)
     name:UITextFieldTextDidChangeNotification
     object:_textView];
    
}

//移除观察者
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:_textView];
}
//实现通知方法
-(void)textFieldTextDidChange:(NSNotification *)notification
{
    UITextField *textfield=[notification object];
    _sendBtn.enabled = textfield.text.length != 0;
    
}

- (void)setUpTextView{
    UITextField *textView = [[UITextField alloc] initWithFrame:[UIScreen mainScreen].bounds];
    textView.height = 200;
    _textView = textView;
    textView.textAlignment = 0;
    textView.placeholder = @"分享新鲜事";
    [self.view addSubview:textView];
}

- (void)setUpNavBar{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *cancleItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
   
    
    self.navigationItem.leftBarButtonItem = cancleItem;
    
    UIButton *composeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [composeBtn setTitle:@"发送" forState:UIControlStateNormal];
    _sendBtn = composeBtn;
    [composeBtn sizeToFit];
    [composeBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [composeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [composeBtn addTarget:self action:@selector(compose) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithCustomView:composeBtn];
    composeBtn.enabled = NO;
    
    self.navigationItem.rightBarButtonItem = composeItem;
}
- (void)cancle{
   
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)compose{
    
//    发送微博
    NSString *url = @"https://open.weibo.cn/2/statuses/update.json";
    NSDictionary *param = @{@"status":_textView.text};
    
    [FXCNetworking postWithURL:url parameter:param success:^(id responseObject) {
        //        添加提示窗
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.labelText = @"发送完成";
        //        延迟处理
        
        [hud hide:YES afterDelay:1];
        [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0];
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
//延迟跳转
- (void)delayMethod{
     [self.navigationController popViewControllerAnimated:YES];
}

@end
